<template>
  <el-dialog :close-on-click-modal="false" :visible.sync="studentAddFormVisible" center title="添加学生" top="15vh"
             width="30%">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="medium">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择">
          <el-option v-for="item in sexSelectList" :key="item.value" :label="item.label"
                     :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-select v-model="form.province" filterable placeholder="请选择">
          <el-option v-for="item in provinceSelectList" :key="item.value" :label="item.label"
                     :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
            v-model="form.birthday"
            placeholder="选择日期"
            type="date"
            value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="身高" prop="height">
        <el-input type="number" min="0" v-model.number="form.height" placeholder="请输入身高"></el-input>
      </el-form-item>
      <el-form-item label="学历" prop="education">
        <el-select v-model="form.education" placeholder="请选择">
          <el-option v-for="item in educationSelectList" :key="item.value" :label="item.label"
                     :value="item.value"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-button @click="resetForm" size="mini">重置</el-button>
        </el-col>
        <el-col :offset="6" :span="12">
          <el-button @click="studentAddFormVisible = false" size="mini">取 消</el-button>
          <el-button type="primary" @click="commit" size="mini">确 定</el-button>
        </el-col>
      </el-row>
    </div>
  </el-dialog>
</template>

<script>
import {regionCodes} from "@/utils/map/regionCodes";

export default {
  data() {
    return {
      form: {
        name: null,
        sex: null,
        province: null,
        birthday: null,
        height: null,
        education: null,
      },
      rules: {
        name: [
          {required: true, message: "请输入姓名", trigger: "blur"}
        ],
        sex: [
          {required: true, message: "请输入性别", trigger: "blur"}
        ],
        province: [
          {required: true, message: "请输入省份", trigger: "blur"}
        ],
        birthday: [
          {required: true, message: "请输入生日", trigger: "blur"}
        ],
        height: [
          {required: true, message: "请输入身高", trigger: "blur"},
          {type: 'number', message: '身高必须为数字值'}
        ],
        education: [
          {required: true, message: "请输入学历", trigger: "blur"}
        ]
      },
      studentAddFormVisible: false,
      sexSelectList: [
        {
          value: 0,
          label: '男'
        },
        {
          value: 1,
          label: '女'
        }
      ],
      educationSelectList: [
        {
          value: 0,
          label: '本科生'
        },
        {
          value: 1,
          label: '硕士生'
        },
        {
          value: 2,
          label: '博士生'
        }
      ],
    }
  },
  computed: {
    provinceSelectList() {
      let provinceList = [];
      for (let propName in regionCodes) {
        if (propName === "中国") {
          continue;
        }
        provinceList.push({
          value: regionCodes[propName].adcode,
          label: propName
        });
      }
      return provinceList;
    },
  },
  methods: {
    showAddStudentForm() {
      this.studentAddFormVisible = true;
    },
    commit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$axios.post("/api/student/add", this.form).then(res => {
            if (res.code === 0) {
              this.$message.success("添加成功");
              this.$emit("success-add");
              this.studentAddFormVisible = false;
            } else {
              if (res.discription !== "") {
                this.$message.error(res.message + "，" + res.description);
              } else {
                this.$message.error(res.message);
              }
            }
          }).catch(err => {
            this.$message.error("添加失败");
          });
        } else {
          this.$message.error("输入内容有误");
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
  },
  mounted() {
    document.body.appendChild(this.$el);
  },
  beforeDestroy() {
  },
  destroyed() {
    if (this.$el && this.$el.parentNode) {
      this.$el.parentNode.removeChild(this.$el);
    }
  }
}
</script>

<style lang="scss" scoped>

</style>